package com.zzptc.sgz.logintest;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.Switch;
import android.widget.TextView;

import com.zzptc.sgz.logintest.Bean.User;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private RadioGroup radioGroup;
    private Switch autoswitch;
    private EditText usnedit,psdedit;
    private Button registerbtn,loginbtn;
    private TextView logtxt;
    int CHOOSE_LEVEL=0;
    int userid;
    List<User> userList=new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        getUser();
        setListener();
    }

    private void getUser() {
        User user=new User(0,"zhangsan","123");
        User user1=new User(1,"lisi","456");
        User user2=new User(2,"wangwu","789");
        userList.add(user);
        userList.add(user1);
        userList.add(user2);
    }

    private boolean login(){
        boolean k=false;
        for (int i=0;i<userList.size();i++){
            User user=userList.get(i);
            if (usnedit.getText().toString().equals(user.getUsername())
                    &&psdedit.getText().toString().equals(user.getPassword())){
                SharedPreferences sp=getSharedPreferences("userinfo",MODE_PRIVATE);
                SharedPreferences.Editor editor=sp.edit();
                editor.putString("username",user.getUsername());
                editor.putString("password",user.getPassword());
                editor.apply();
                userid=user.getId();
                k=true;
                break;
            }
        }
        return k;
    }
    private void setListener() {
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId){
                    case R.id.adminbtn:
                        CHOOSE_LEVEL=1;
                        break;
                    case R.id.userbtn:
                        CHOOSE_LEVEL=2;
                        break;

                }
            }
        });






        autoswitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (buttonView.isChecked()){
                    SharedPreferences sp=getSharedPreferences("userinfo",MODE_PRIVATE);
                    usnedit.setText(sp.getString("username",""));
                    psdedit.setText(sp.getString("password",""));
                }else {
                    usnedit.setText("");
                    psdedit.setText("");
                }
            }
        });
        loginbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (login()){
                    Intent intent=new Intent(MainActivity.this,SuccessActivity.class);
                    intent.putExtra("userid",userid);
                    startActivity(intent);
                }else {
                    logtxt.setText("登陆失败");
                }
            }
        });
        registerbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(MainActivity.this,RegisterActivity.class);
                startActivityForResult(intent,1);
            }
        });
    }

    private void initView() {
        radioGroup=findViewById(R.id.radiogroup);
        autoswitch=findViewById(R.id.autoswitch);
        usnedit=findViewById(R.id.usnedit);
        psdedit=findViewById(R.id.psdedit);
        registerbtn=findViewById(R.id.registerbtn);
        loginbtn=findViewById(R.id.loginbtn);
        logtxt=findViewById(R.id.logtxt);
    }


    private void register(String usn,String psd){
        boolean k=true;
        for (User u:userList){
            if (usn.equals(u.getUsername())) {
                k = false;
            }
        }
        if (k){
            User user=new User();
            user.setId(userList.size());
            user.setUsername(usn);
            user.setPassword(psd);
            userList.add(user);
            logtxt.setText("注册成功");
        }else {
            logtxt.setText("账号重复");
        }

    }

    @Override
    protected void onActivityResult(int requestCode,int resultCode,Intent intent) {
        super.onActivityResult(requestCode, resultCode, intent);
        if (requestCode==1&&resultCode==2){
            String usn=intent.getStringExtra("username");
            String psd=intent.getStringExtra("password");
            register(usn,psd);
        }

    }

}
